package com.maoye.ossp.sys.news.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.hotent.base.api.query.QueryOP;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.news.entity.News;
import com.maoye.ossp.sys.news.entity.NewsCategory;
import com.maoye.ossp.sys.news.entity.NewsCriteria;
import com.maoye.ossp.sys.news.service.INewsCategoryService;
import com.maoye.ossp.sys.news.service.INewsCriteriaService;
import com.maoye.ossp.sys.news.service.INewsService;
import com.maoye.ossp.sys.org.util.UserUtils;

/**
 * 新闻种类Controller
 * @author yanzy
 *
 */
@Controller
@RequestMapping(value = "/sys/news/category")
public class NewsCategoryController extends BaseController{
	
	@Resource
	private INewsCategoryService InewsCategoryService;
	
	@Resource
	private INewsCriteriaService InewsCriteriaService;
	
	/**
	 * 新闻种类列表
	 * @param newsCategory
	 * @param request
	 * @param model
	 * @param response
	 * 
	 */
	@RequestMapping(value = "/list")
	public String list(NewsCategory newsCategory,HttpServletRequest request,
			Model model,HttpServletResponse response){
		QueryFilter queryFilter = getQuerFilter(request,newsCategory);
		Page<NewsCategory> page = (Page<NewsCategory>)InewsCategoryService
				.find(queryFilter);
		model.addAttribute("page", page);
		return "/sys/news/category/list";
	}
	
	/**
	 * 生成新闻类别树
	 * @param response
	 * @throws IOException
	 * 
	 */
	@RequestMapping(value = "/categoryTree")
	public void CategoryTree(HttpServletResponse response) throws IOException{
		List<NewsCategory> list = InewsCategoryService.getAll();
		List<String> lstTree = new ArrayList<String>();
		for (NewsCategory newsCategory : list) {
			String jonstr = "{id:'"+newsCategory.getId()+"',pId:'"+
			newsCategory.getParentId()+"', name:\""+newsCategory.getName()+"\" , open:true}";
			lstTree.add(jonstr);
		}
		JSONArray json = new JSONArray();
		String array = json.fromObject(lstTree).toString();
		PrintWriter out = response.getWriter();
		out.print(array);
		out.flush();
		out.close();
	}
	
	/**
	 * 根据类别获得新闻
	 * @param request
	 * @param model
	 * @param response
	 * 
	 */
	@RequestMapping(value = "/getByCondition")
	public String getByCondition(NewsCriteria newsCriteria,HttpServletRequest request,Model model,
			HttpServletResponse response){
		String category = request.getParameter("category");
		QueryFilter queryFilter = getQuerFilter(request,newsCriteria);
		if(category!=null ||category !=""){
			queryFilter.addFilter("category", category, QueryOP.EQUAL);
		}		
		Page<NewsCriteria> page = (Page<NewsCriteria>)InewsCriteriaService
				.find(queryFilter);
		model.addAttribute("page", page);
		return "/sys/news/category/innerList";
	}
	
	/**
	 * 根据ID删除新闻信息
	 * @param id
	 * @param request
	 * @param redirectAttributes
	 * 
	 */
	@RequestMapping(value = "/{id}/delete")
	public String delete(@PathVariable String id,HttpServletRequest request,
			RedirectAttributes redirectAttributes){
		InewsCriteriaService.remove(id);
		addMessage(redirectAttributes, "删除新闻成功");
		return "redirect:/sys/news/category/getByCondition";
	}
	
	/**
	 * 根据ID获得详细信息
	 * @param id
	 * @param model
	 * 
	 */
	@RequestMapping(value = "/{id}/view")
	public String showDetail(@PathVariable String id,Model model){
		NewsCriteria newsCriteria = InewsCriteriaService.get(id);
		model.addAttribute("newsCriteria",newsCriteria);
		return "/sys/news/category/view";
	}
	
	/**
	 * 跳转添加新闻类型页面
	 * @param request
	 * @param model
	 * @param newsCategory
	 * 
	 */
	@RequestMapping(value = "/addCategory")
	public String addCategory(NewsCategory newsCategory,
			HttpServletRequest request,Model model){
		String categoryId = request.getParameter("categoryId");
		if(categoryId!=null&&!"".equals(categoryId)){
			newsCategory = InewsCategoryService.get(categoryId);
		}
		model.addAttribute("category", newsCategory);
		return "/sys/news/category/add";
	}
	
	/**
	 * 保存新闻类别
	 * @param request
	 * @param model
	 * @param newsCategory
	 * @param redirectAttributes
	 * 
	 */
	@RequestMapping(value = "/save")
	public String saveCategory(HttpServletRequest request,Model model,
			NewsCategory newsCategory,RedirectAttributes redirectAttributes){
		String parentId = newsCategory.getParentId();
		Long id = idGenerator.getUId();
		newsCategory.setId(id);
		//如果没有父ID,则为根节点      
		if("".equals(parentId)){
			newsCategory.setParentId(null);
			newsCategory.setCategoryId(id.toString());
		}
		if(parentId!=null&&!"".equals(parentId)){
			//层级ID
			NewsCategory parentCategory = InewsCategoryService.get(parentId);
			String hierarchyId = parentCategory.getCategoryId()+"!"+id;
			newsCategory.setCategoryId(hierarchyId);
		}
		newsCategory.setCreateBy(UserUtils.getCurrentUserId().toString());
		InewsCategoryService.saveNewsCategory(newsCategory);
		addMessage(redirectAttributes, "保存新闻类别成功");
		return "redirect:/sys/news/category/list";
	}
	
	
}
